using System;
using System.Collections;
using System.Net.Mail;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Jfc.DnnMarketingTools.MemberInvite
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditMemberInvite class is used to manage content
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class QuickInvite : PortalModuleBase
    {

        #region Event Handlers

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                //If this is the first visit to the page, bind the role data to the datalist
                if (Page.IsPostBack == false)
                {
                    lblSuccess.Visible = false;
                    lblError.Visible = false;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdSend_Click runs when the send button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdInvite_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                lblSuccess.Visible = false;
                lblError.Visible = false;

                MemberInviteController objMemberInvites = new MemberInviteController();
                MemberInviteInfo objMemberInvite = new MemberInviteInfo();

                //objMemberInvite.InviteId = _ItemId;
                objMemberInvite.PortalId = PortalId;
                objMemberInvite.FirstName = txtFirstName.Text.Trim();
                objMemberInvite.LastName = txtLastName.Text.Trim();
                objMemberInvite.Email = txtEmail.Text.Trim();

                MemberInviteSettingsController controller = new MemberInviteSettingsController();
                MemberInviteSettingsInfo memberInviteSettings = controller.GetMemberInviteSettings(PortalId, UserId);
                
                objMemberInvite.Subject = memberInviteSettings.SubjectTemplate;
                objMemberInvite.Body = memberInviteSettings.BodyTemplate;

                objMemberInvite.CreatedByUser = UserId;
                objMemberInvite.SetStatus(StatusType.Registered);
                objMemberInvite.InviteGuid = Guid.NewGuid();


                // Raise the event
                //objMemberInvites.OnMemberInvited( PortalId);
                // Send Invitation
                ModuleController objModules = new ModuleController();
                Hashtable settings = objModules.GetTabModuleSettings(TabModuleId);

                SenderAddressType senderAddress = SenderAddressType.User;
                senderAddress = (SenderAddressType)Convert.ToInt32(Null.GetNull(settings["SenderAddress"], senderAddress));

                objMemberInvites.Send(objMemberInvite, senderAddress, PortalSettings, UserInfo);

                //Update the content within the MemberInvite table
                objMemberInvites.AddMemberInvite(objMemberInvite);

                lblSuccess.Visible = true;

                //refresh cache
#pragma warning disable 0618
                SynchronizeModule();
                //ModuleController.SynchronizeModule(ModuleId);
#pragma warning restore
                
                //Redirect back to the portal home page
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                lblSuccess.Visible = false;
                lblError.Visible = true;
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }
}

